<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;
class Post extends Base{
    public function index(){
        $category_list=Db::name('category')->where('pid',0)->select();
        $this->assign('category_list',$category_list);
        return $this->fetch('post');
    }
    public function cate_b(){
        $pid=input('pid');
        $cate_b=Db::name('category')->where('pid',$pid)->select();
        if (count($cate_b)==0){
            return '0';
        }else {
            return $cate_b;
        }
        //$this->assign('cate_b',$cate_b);
        //return $this->fetch('post');
    }
    public function post(){
        //var_dump(Session::pull('imgsrc'));
        $category_m_id=input('category_m_id');//主节点
        $category_b_id=input('category_b_id');//根节点
        $category=!$category_b_id?$category_m_id:$category_b_id;
        $title=input('title');
        $content=input('content');
        $tag=input('tag');
        $publictime=date("Y-m-d H:i:s");
        $intro=substr(input('intro'),0,350);
        $img_temp=Session::pull('imgsrc');
        //var_dump($img_temp);
        $imgsrc=!$img_temp?("/public/upload/default.jpg"):$img_temp;
        $data=[
            'title'=>$title,
            'content'=>$content,
            'tag'=>$tag,
            'publictime'=>$publictime,
            'intro'=>$intro,
            'username'=>"管理员",
            'imgsrc'=>$imgsrc,
            'category'=>$category,
            'pv'=>0
        ];
        Db::name('article')->insert($data);
        return 'success';
    }

    public function upload(){
        $request=Request::instance();
        $file=$request->file('file');
        //echo $file->getInfo('name');
        $info=$file->move(ROOT_PATH . 'public' . DS . 'upload');
        $src="/public/upload/".$info->getSaveName();
        Session::set('imgsrc',$src);
        //return $src;
        //echo $src;
        //var_dump($info);
    }
}